#include "main.h"
#include "RGB.h"
#include "tim.h"
#include "stdio.h"
#include "KeyBoard.h"
#include "BTK05.h"
#include "string.h"

const RGB_Color_TypeDef RED     = {63,0,0};
const RGB_Color_TypeDef GREEN   = {0,63,0};
const RGB_Color_TypeDef BLUE    = {0,0,63};

const RGB_Color_TypeDef SKY          = {0,63,63};
const RGB_Color_TypeDef MAGENTA      = {63,0,55};
const RGB_Color_TypeDef YELLOW       = {127,216,0};
const RGB_Color_TypeDef ORANGE       = {42,35,0};
const RGB_Color_TypeDef BLACK        = {0,0,0};
const RGB_Color_TypeDef LIGHT_GREEN  = {47,63,15};
const RGB_Color_TypeDef WHITE        = {63,63,63};
const RGB_Color_TypeDef LIGHT_PINK   = {63,43,47};
const RGB_Color_TypeDef DEEP_PINK    = {63,5,36};
const RGB_Color_TypeDef BAMBOO_GREEN = {0,41,40};
const RGB_Color_TypeDef LIGHT_PURPLE = {37,29,51};

RGB_Color_TypeDef RGB_STATE_PRE;
RGB_Color_TypeDef RGB_RING[87];


RGB_Color_TypeDef RGB_STATE;

uint32_t Pixel_Buf[Pixel_NUM][24];

uint8_t Pre_index = 0;

uint8_t Count_Time_RGB = 0;
uint8_t Count_RGB_Flag = 0;
uint8_t RGB_Change_Flag = 1;

uint8_t Up_Flag_0 = 1;
uint8_t Down_Flag_0 = 0;
uint8_t Up_Flag_1 = 1;
uint8_t Down_Flag_1 = 0;
uint8_t Up_Flag_2 = 1;
uint8_t Down_Flag_2 = 0;
uint8_t Up_Flag_3 = 1;
uint8_t Down_Flag_3 = 0;
uint8_t Up_Flag_4 = 0;
uint8_t Down_Flag_4 = 1;
uint8_t Up_Flag_5 = 1;
uint8_t Down_Flag_5 = 0;

uint8_t Temp_0 = 0;
uint8_t Temp_1 = 0;
uint8_t Temp_2 = 0;
uint8_t Temp_3 = 0;
uint8_t Temp_4 = 0;
uint8_t Temp_5 = 0;

uint8_t RGB_PWM_Index_COL = 0;
uint8_t RGB_PWM_Index_ROW = 0;
uint8_t RGB_PWM_Breath = 0;

uint8_t Up_Flag_Row = 1;
uint8_t Down_Flag_Row = 0;

uint8_t Up_Flag_Breath = 1;
uint8_t Down_Flag_Breath = 0;

uint8_t Temp_Col0 = 0;
uint8_t Temp_Col1 = 0;
uint8_t Temp_Col2 = 0;
uint8_t Temp_Col3 = 0;
uint8_t Temp_Col4 = 0;
uint8_t Temp_Col5 = 0;

COLOR_TypeDef Pre_ID[87];
COLOR_TypeDef NOW_ID_0[87];
COLOR_TypeDef NOW_ID_1[87];
COLOR_TypeDef NOW_ID_2[87];

COLOR_TypeDef Choose_Color_ID[88];
uint8_t Color_Flag = BLACK_COLOR_STATE;
uint8_t COLOR_CHOOSE_R = 0;
uint8_t COLOR_CHOOSE_G = 0;
uint8_t COLOR_CHOOSE_B = 0;

uint8_t COLOR_RING_BUFFER_1[24] = {2,4,6,8,8,8,8,8,8,8,8,8,6,4,2,1,1,1,1,1,1,1,1,1};
uint8_t COLOR_RING_BUFFER_2[24] = {2,4,6,8,8,8,8,8,8,8,8,8,6,4,2,3,3,3,3,3,3,3,3,3};
uint8_t COLOR_RING_BUFFER_3[24] = {2,4,6,8,8,8,8,8,8,8,8,8,6,4,2,5,5,5,5,5,5,5,5,5};
uint8_t COLOR_RING_BUFFER_4[24] = {2,4,6,8,8,8,8,8,8,8,8,8,6,4,2,7,7,7,7,7,7,7,7,7};
	
uint8_t Control_Array[8];

uint8_t sequence[87] = 
{
	0x29,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x4B,
	0x4A,0x49,0x2A,0x2E,0x2D,0x27,0x26,0x25,0x24,0x23,0x22,0x21,0x20,0x1F,0x1E,0x35,
	0x2B,0x14,0x1A,0x08,0x15,0x17,0x1C,0x18,0x0C,0x12,0x13,0x2F,0x30,0x31,0x4C,0x4D,0x4E,
	0x28,0x34,0x33,0x0F,0x0E,0x0D,0x0B,0x0A,0x09,0x07,0x16,0x04,0x39,
	0x00,0x1D,0x1B,0x06,0x19,0x05,0x11,0x10,0x36,0x37,0x38,0x00,0x52,
	0x4F,0x51,0x50,0x00,0x00,0x00,0x00,0x2C,0x00,0x00,0x00
};

uint8_t sequence_1[6][17] = 
{
	0x29,0x3A,0x3B,0x3C,0x3D,0x3E,0x3F,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x00,
	0x4B,0x4A,0x49,0x2A,0x2E,0x2D,0x27,0x26,0x25,0x24,0x23,0x22,0x21,0x20,0x1F,0x1E,0x35,
	0x2B,0x14,0x1A,0x08,0x15,0x17,0x1C,0x18,0x0C,0x12,0x13,0x2F,0x30,0x31,0x4C,0x4D,0x4E,
	0x28,0x34,0x33,0x0F,0x0E,0x0D,0x0B,0x0A,0x09,0x07,0x16,0x04,0x39,0x00,0x00,0x00,0x00,
	0x00,0x1D,0x1B,0x06,0x19,0x05,0x11,0x10,0x36,0x37,0x38,0x00,0x52,0x00,0x00,0x00,0x00,
	0x4F,0x51,0x50,0x00,0x00,0x00,0x00,0x2C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

void RGB_SetColor(uint8_t LED_ID,RGB_Color_TypeDef Color)
{
	uint8_t i;
	if(LED_ID >= Pixel_NUM)
	{
		return;
	}
	
	for(i = 0;i < 8;i++)
	{
		Pixel_Buf[LED_ID][i] = ((Color.G & ( 1 << (7 - i)))?(CODE_1):CODE_0);
	}
	for(i = 8;i < 16;i++)
	{
		Pixel_Buf[LED_ID][i] = ((Color.R & ( 1 << (15 - i)))?(CODE_1):CODE_0);
	}
	for(i = 16;i < 24;i++)
	{
		Pixel_Buf[LED_ID][i] = ((Color.B & ( 1 << (23 - i)))?(CODE_1):CODE_0);
	}
}


void RGB_Set_Color(uint8_t LED_ID,uint8_t R,uint8_t G,uint8_t B)
{
	uint8_t i;
	if(LED_ID >= Pixel_NUM)
	{
		return;
	}
	for(i = 0;i < 8;i++)
	{
		Pixel_Buf[LED_ID][i] = ((G & ( 1 << (7 - i)))?(CODE_1):CODE_0);
	}
	for(i = 8;i < 16;i++)
	{
		Pixel_Buf[LED_ID][i] = ((R & ( 1 << (15 - i)))?(CODE_1):CODE_0);
	}
	for(i = 16;i < 24;i++)
	{
		Pixel_Buf[LED_ID][i] = ((B & ( 1 << (23 - i)))?(CODE_1):CODE_0);
	}
}

void RGB_SendArray(void)
{
	HAL_TIM_PWM_Start_DMA(&htim1,TIM_CHANNEL_1,(uint32_t *)Pixel_Buf,(Pixel_NUM+1)*24);
	__HAL_TIM_SetCompare(&htim1, TIM_CHANNEL_1, 0);
}

void Full_COLOR(RGB_Color_TypeDef Color)
{
	uint16_t i;
	for(i = 0;i < Pixel_NUM;i++)
	{
		RGB_SetColor(i,Color);
	}
	RGB_SendArray();
}	

void RGB_SetLen(uint16_t Pixel_Len,RGB_Color_TypeDef Color)
{
	uint16_t i;
	for(i = 0;i < Pixel_Len;i++)
	{
		RGB_SetColor(i,Color);
	}
	RGB_SendArray();
	
}

void RGB_Set_Point(uint16_t id,RGB_Color_TypeDef Color)
{
	Pre_ID[Pre_index].id = id;
	Pre_index++;
	if(Pre_ID[Pre_index - 2].id == Pre_ID[Pre_index - 1].id && Pre_index > 1)
	{
		Pre_ID[Pre_index-1].id = 0;
		Pre_index--;
	}
	
	for(int index = 0;index < id;index++)
	{
		RGB_SetColor(index,BLACK);
		
	}
	for(int index = 0;Pre_ID[index].id != 0;index++)
	{
		RGB_SetColor(Pre_ID[index].id,Color);
	}
	RGB_SetColor(id,Color);
	RGB_SendArray();
}

void RGB_Set_Point_Particular(uint16_t id,uint8_t R,uint8_t G,uint8_t B)
{
	Pre_ID[Pre_index].id = id;
	Pre_index++;
	if(Pre_ID[Pre_index - 2].id == Pre_ID[Pre_index - 1].id && Pre_index > 1)//找之前的按键
	{
		Pre_ID[Pre_index-1].id = 0;
		Pre_index--;
	}
	
	for(int index = 0;index < id;index++)//把没有的变成黑色
	{
		RGB_SetColor(index,BLACK);
	}
	for(int index = 0;Pre_ID[index].id != 0;index++)
	{
		RGB_Set_Color(Pre_ID[index].id,R,G,B);
	}
	RGB_Set_Color(id,R,G,B);
}

void RGB_Set_Point_Row_inorder(uint8_t row,uint8_t col,uint8_t R,uint8_t G,uint8_t B)
{
	if(row == 0)//如果是第一行
	{
		RGB_Set_Point_Particular(col,R,G,B);
	}
	else if(row == 1)//如果是第二行
	{
		RGB_Set_Point_Particular(16+16-col,R,G,B);
	}
	else if(row == 2)
	{
		RGB_Set_Point_Particular(33+col,R,G,B);
	}
	else if(row == 3)
	{
		RGB_Set_Point_Particular(50+12-col,R,G,B);
	}
	else if(row == 4)
	{
		RGB_Set_Point_Particular(63+col,R,G,B);
	}
	else if(row == 5)
	{
		RGB_Set_Point_Particular(76+10-col,R,G,B);
	}
	RGB_SendArray();
}

void RGB_Set_Point_Row(uint8_t row,uint8_t col,RGB_Color_TypeDef Color)
{
	if(row == 0)//如果是第一行
	{
		RGB_Set_Point(col,Color);
	}
	else if(row == 1)//如果是第二行
	{
		RGB_Set_Point(16+col,Color);
	}
	else if(row == 2)
	{
		RGB_Set_Point(33+col,Color);
	}
	else if(row == 3)
	{
		RGB_Set_Point(50+col,Color);
	}
	else if(row == 4)
	{
		RGB_Set_Point(63+col,Color);
	}
	else if(row == 5)
	{
		RGB_Set_Point(76+col,Color);
	}
}



void Water_Flow_COL(RGB_Color_TypeDef Color)
{
	if(Count_RGB_Flag == 1)
	{
		HAL_TIM_Base_Start_IT(&htim3);
		
		if(Point_Water_Flag == 2)
		{
			Color.R = Color.R / 3;
			Color.G = Color.G / 3;
			Color.B = Color.B / 3;
		}
		
		if(RGB_Change_Flag == 1)
		{
			RGB_Change_Flag = 0;
			RGB_PWM_Index_COL++;
			if(RGB_PWM_Index_COL > 16)
			{
				RGB_PWM_Index_COL = 0;
			}
			uint8_t i_row1 = 6; 
			uint8_t i_row2 = 5;
			uint8_t i_row3 = 6;
			uint8_t i_row4 = 2;
			uint8_t i_row5 = 7;
			uint8_t i_row6 = 7;
			for(uint8_t index = 0;index < 17;index++)
			{
				if(Down_Flag_0 == 1)
				{
					i_row1--;
				}
				if(Up_Flag_0 == 1)
				{
					i_row1++;
				}
				if(i_row1 == 8)
				{
					Down_Flag_0 = 1;
					Up_Flag_0 = 0;
				}
				else if(i_row1 == 0)
				{
					Up_Flag_0 = 1;
					Down_Flag_0 = 0;
				}
				if(index + RGB_PWM_Index_COL > 15)
				{
					Temp_0 = index + RGB_PWM_Index_COL - 16;
				}
				else
				{
					Temp_0 = index + RGB_PWM_Index_COL;
				}
				if(Temp_0 < 16)
				{
					NOW_ID_1[Temp_0].PWM_INDEX = i_row1;
					NOW_ID_1[Temp_0].R = Color.R;
					NOW_ID_1[Temp_0].G = Color.G;
					NOW_ID_1[Temp_0].B = Color.B;	
					RGB_Set_Color(Temp_0,NOW_ID_1[Temp_0].R/8*NOW_ID_1[Temp_0].PWM_INDEX,NOW_ID_1[Temp_0].G/8*NOW_ID_1[Temp_0].PWM_INDEX,NOW_ID_1[Temp_0].B/8*NOW_ID_1[Temp_0].PWM_INDEX);
				}
			}
			for(uint8_t index = 16;index < 33;index++)
			{
				if(Down_Flag_1 == 1)
				{
					i_row2--;
				}
				if(Up_Flag_1 == 1)
				{
					i_row2++;
				}
				if(i_row2 == 8)
				{
					Up_Flag_1 = 0;
					Down_Flag_1 = 1;
				}
				else if(i_row2 == 0)
				{
					Down_Flag_1 = 0;
					Up_Flag_1 = 1;
				}
				if(index + RGB_PWM_Index_COL > 32)
				{
					Temp_1 = index + RGB_PWM_Index_COL - 17;
				}
				else
				{
					Temp_1 = index + RGB_PWM_Index_COL;
				}
				if(48 - Temp_1 < 33)
				{
					NOW_ID_1[48-Temp_1].PWM_INDEX = i_row2;
					NOW_ID_1[48-Temp_1].R = Color.R;
					NOW_ID_1[48-Temp_1].G = Color.G;
					NOW_ID_1[48-Temp_1].B = Color.B;	
					RGB_Set_Color(48-Temp_1,NOW_ID_1[48-Temp_1].R/8*NOW_ID_1[48-Temp_1].PWM_INDEX,NOW_ID_1[48-Temp_1].G/8*NOW_ID_1[48-Temp_1].PWM_INDEX,NOW_ID_1[48-Temp_1].B/8*NOW_ID_1[48-Temp_1].PWM_INDEX);
				}
			}
			for(uint8_t index = 33;index < 50;index++)
			{
				if(Down_Flag_2 == 1)
				{
					i_row3--;
				}
				if(Up_Flag_2 == 1)
				{
					i_row3++;
				}
				if(i_row3 == 8)
				{
					Up_Flag_2 = 0;
					Down_Flag_2 = 1;
				}
				else if(i_row3 == 0)
				{
					Up_Flag_2 = 1;
					Down_Flag_2 = 0;
				}
				if(index + RGB_PWM_Index_COL > 49)
				{
					Temp_2 = index + RGB_PWM_Index_COL - 17;
				}
				else
				{
					Temp_2 = index + RGB_PWM_Index_COL;
				}
				if(Temp_2 < 50)
				{
					NOW_ID_1[Temp_2].PWM_INDEX = i_row3;
					NOW_ID_1[Temp_2].R = Color.R;
					NOW_ID_1[Temp_2].G = Color.G;
					NOW_ID_1[Temp_2].B = Color.B;	
					RGB_Set_Color(Temp_2,NOW_ID_1[Temp_2].R/8*NOW_ID_1[Temp_2].PWM_INDEX,NOW_ID_1[Temp_2].G/8*NOW_ID_1[Temp_2].PWM_INDEX,NOW_ID_1[Temp_2].B/8*NOW_ID_1[Temp_2].PWM_INDEX);
				}
			}
			for(uint8_t index = 50;index < 68;index++)
			{
				if(Down_Flag_3 == 1)
				{
					i_row4--;
				}
				if(Up_Flag_3 == 1)
				{
					i_row4++;
				}
				if(i_row4 == 8)
				{
					Up_Flag_3 = 0;
					Down_Flag_3 = 1;
				}
				else if(i_row4 == 0)
				{
					Up_Flag_3 = 1;
					Down_Flag_3 = 0;
				}
				if(index + RGB_PWM_Index_COL > 67)
				{
					Temp_3 = index + RGB_PWM_Index_COL - 17;
				}
				else
				{
					Temp_3 = index + RGB_PWM_Index_COL;
				}
				if(117 - Temp_3 < 63)
				{
					NOW_ID_1[117 - Temp_3].PWM_INDEX = i_row4;
					NOW_ID_1[117 - Temp_3].R = Color.R;
					NOW_ID_1[117 - Temp_3].G = Color.G;
					NOW_ID_1[117 - Temp_3].B = Color.B;
					RGB_Set_Color(117 - Temp_3,NOW_ID_1[117 - Temp_3].R/8*NOW_ID_1[117 - Temp_3].PWM_INDEX,NOW_ID_1[117 - Temp_3].G/8*NOW_ID_1[117 - Temp_3].PWM_INDEX,NOW_ID_1[117 - Temp_3].B/8*NOW_ID_1[117 - Temp_3].PWM_INDEX);
				}
			}
			for(uint8_t index = 63;index < 80;index++)
			{
				if(Down_Flag_4 == 1)
				{
					i_row5--;
				}
				if(Up_Flag_4 == 1)
				{
					i_row5++;
				}
				if(i_row5 == 8)
				{
					Up_Flag_4 = 0;
					Down_Flag_4 = 1;
				}
				else if(i_row5 == 0)
				{
					Up_Flag_4 = 1;
					Down_Flag_4 = 0;
				}
				if(index + RGB_PWM_Index_COL > 79)
				{
					Temp_4 = index + RGB_PWM_Index_COL - 17;
				}
				else
				{
					Temp_4 = index + RGB_PWM_Index_COL;
				}
				if(Temp_4 < 76)
				{
					NOW_ID_1[Temp_4].PWM_INDEX = i_row5;
					NOW_ID_1[Temp_4].R = Color.R;
					NOW_ID_1[Temp_4].G = Color.G;
					NOW_ID_1[Temp_4].B = Color.B;
					RGB_Set_Color(Temp_4,NOW_ID_1[Temp_4].R/8*NOW_ID_1[Temp_4].PWM_INDEX,NOW_ID_1[Temp_4].G/8*NOW_ID_1[Temp_4].PWM_INDEX,NOW_ID_1[Temp_4].B/8*NOW_ID_1[Temp_4].PWM_INDEX);
				}
			}
			for(uint8_t index = 76;index < 93;index++)
			{
				if(Down_Flag_5 == 1)
				{
					i_row6--;
				}
				if(Up_Flag_5 == 1)
				{
					i_row6++;
				}
				if(i_row6 == 8)
				{
					Up_Flag_5 = 0;
					Down_Flag_5 = 1;
				}
				else if(i_row6 == 0)
				{
					Up_Flag_5 = 1;
					Down_Flag_5 = 0;
				}
				if(index + RGB_PWM_Index_COL > 92)
				{
					Temp_5 = index + RGB_PWM_Index_COL - 17;
				}
				else
				{
					Temp_5 = index + RGB_PWM_Index_COL;
				}
				if(163 - Temp_5 < 87 && 163 - Temp_5 > 75)
				{
					NOW_ID_1[163 - Temp_5].PWM_INDEX = i_row6;
					NOW_ID_1[163 - Temp_5].R = Color.R;
					NOW_ID_1[163 - Temp_5].G = Color.G;
					NOW_ID_1[163 - Temp_5].B = Color.B;
					RGB_Set_Color(163 - Temp_5,NOW_ID_1[163 - Temp_5].R/8*NOW_ID_1[163 - Temp_5].PWM_INDEX,NOW_ID_1[163 - Temp_5].G/8*NOW_ID_1[163 - Temp_5].PWM_INDEX,NOW_ID_1[163 - Temp_5].B/8*NOW_ID_1[163 - Temp_5].PWM_INDEX);
				}
			}
			if(Point_Water_Flag == 2)
			{
				Point_Water(Color);
			}
			RGB_SendArray();
			memset(NOW_ID_1,0,sizeof(NOW_ID_1));
		}
	}
}

void Breath(RGB_Color_TypeDef Color)
{
	if(Count_RGB_Flag == 2)
	{
		HAL_TIM_Base_Start_IT(&htim3);
		
		if(Point_Water_Flag == 2)
		{
			Color.R = Color.R / 3;
			Color.G = Color.G / 3;
			Color.B = Color.B / 3;
		}
		
		if(RGB_Change_Flag == 1)
		{
			RGB_Change_Flag = 0;
			if(Up_Flag_Breath == 1)
			{
				RGB_PWM_Breath++;
			}
			if(Down_Flag_Breath == 1)
			{
				RGB_PWM_Breath--;
			}
			if(RGB_PWM_Breath == 16)
			{
				Up_Flag_Breath = 0;
				Down_Flag_Breath = 1;
			}
			if(RGB_PWM_Breath == 0)
			{
				Up_Flag_Breath = 1;
				Down_Flag_Breath = 0;
			}
			for(uint8_t index = 0;index < 87;index++)
			{
				NOW_ID_1[index].PWM_INDEX = RGB_PWM_Breath;
				NOW_ID_1[index].R = Color.R;
				NOW_ID_1[index].G = Color.G;
				NOW_ID_1[index].B = Color.B;
			}
			for(uint8_t i = 0;i < 17;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			for(uint8_t i = 17;i < 33;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			for(uint8_t i = 33;i < 50;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			for(uint8_t i = 50;i < 63;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			for(uint8_t i = 63;i < 76;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			for(uint8_t i = 76;i < 87;i++)
			{
				RGB_Set_Color(i,NOW_ID_1[i].R * (RGB_PWM_Breath)/16,NOW_ID_1[i].G * (RGB_PWM_Breath)/16,NOW_ID_1[i].B * (RGB_PWM_Breath)/16);
			}
			if(Point_Water_Flag == 2)
			{
				Point_Water(Color);
			}
			RGB_SendArray();
			memset(NOW_ID_1,0,sizeof(NOW_ID_1));
		}
	}
}

void Water_Flow_ROW(RGB_Color_TypeDef Color)
{
	if(Count_RGB_Flag == 3)
	{
		HAL_TIM_Base_Start_IT(&htim3);
		
		if(Point_Water_Flag == 2)
		{
			Color.R = Color.R / 3;
			Color.G = Color.G / 3;
			Color.B = Color.B / 3;
		}
		
		uint8_t R = Color.R;
		uint8_t G = Color.G;
		uint8_t B = Color.B;
		
		if(RGB_Change_Flag == 1)
		{
			RGB_Change_Flag = 0;
			if(RGB_PWM_Index_ROW == 32)
			{
				Down_Flag_Row = 1;
				Up_Flag_Row = 0;
			}
			if(RGB_PWM_Index_ROW == 0)
			{
				Down_Flag_Row = 0;
				Up_Flag_Row = 1;
			}
			if(Down_Flag_Row == 1)
			{
				RGB_PWM_Index_ROW--;
			}	
			else if(Up_Flag_Row == 1)
			{
				RGB_PWM_Index_ROW++;
			}
			
			for(uint8_t i = 0;i < 17;i++)
			{
				if(RGB_PWM_Index_ROW - 6 < 0)
				{
					Temp_Col0 = -(RGB_PWM_Index_ROW - 6);
				}
				else
				{
					Temp_Col0 = RGB_PWM_Index_ROW - 6;
				}
				//RGB_Set_Color(i,R*Temp_Col0/16,G*Temp_Col0/16,B*Temp_Col0/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col0;
			}
			for(uint8_t i = 17;i < 33;i++)
			{
				if(RGB_PWM_Index_ROW - 7 < 0)
				{
					Temp_Col1 = -(RGB_PWM_Index_ROW - 7);
				}
				else
				{
					Temp_Col1 = RGB_PWM_Index_ROW - 7;
				}
				//RGB_Set_Color(i,R*Temp_Col1/16,G*Temp_Col1/16,B*Temp_Col1/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col1;
			}
			for(uint8_t i = 33;i < 50;i++)
			{
				if(RGB_PWM_Index_ROW - 8 < 0)
				{
					Temp_Col2 = -(RGB_PWM_Index_ROW - 8);
				}
				else
				{
					Temp_Col2 = RGB_PWM_Index_ROW - 8;
				}
				//RGB_Set_Color(i,R*Temp_Col2/16,G*Temp_Col2/16,B*Temp_Col2/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col2;
			}
			for(uint8_t i = 50;i < 63;i++)
			{
				if(RGB_PWM_Index_ROW - 9 < 0)
				{
					Temp_Col3 = -(RGB_PWM_Index_ROW - 9);
				}
				else
				{
					Temp_Col3 = RGB_PWM_Index_ROW - 9;
				}
				//RGB_Set_Color(i,R*Temp_Col3/16,G*Temp_Col3/16,B*Temp_Col3/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col3;
			}
			for(uint8_t i = 63;i < 76;i++)
			{
				if(RGB_PWM_Index_ROW - 10 < 0)
				{
					Temp_Col4 = -(RGB_PWM_Index_ROW - 10);
				}
				else
				{
					Temp_Col4 = RGB_PWM_Index_ROW - 10;
				}
				//RGB_Set_Color(i,R*Temp_Col4/16,G*Temp_Col4/16,B*Temp_Col4/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col4;
			}
			for(uint8_t i = 76;i < 87;i++)
			{
				if(RGB_PWM_Index_ROW - 11 < 0)
				{
					Temp_Col5 = -(RGB_PWM_Index_ROW - 11);
				}
				else
				{
					Temp_Col5 = RGB_PWM_Index_ROW - 11;
				}
				//RGB_Set_Color(i,R*Temp_Col5/16,G*Temp_Col5/16,B*Temp_Col5/16);
				NOW_ID_1[i].R = R;
				NOW_ID_1[i].G = G;
				NOW_ID_1[i].B = B;
				NOW_ID_1[i].PWM_INDEX = Temp_Col5;
			}
			for(int index = 0;index < 87;index++)
			{
				for(int i = 0;i < 8;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_1[index].G * NOW_ID_1[index].PWM_INDEX / 16 & ( 1 << (7 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 8;i < 16;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_1[index].R * NOW_ID_1[index].PWM_INDEX / 16 & ( 1 << (15 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 16;i < 24;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_1[index].B * NOW_ID_1[index].PWM_INDEX / 16 & ( 1 << (23 - i)))?(CODE_1):CODE_0);
				}
			}
			if(Point_Water_Flag == 2)
			{
				Point_Water(Color);
			}
			RGB_SendArray();
			memset(NOW_ID_1,0,sizeof(NOW_ID_1));
		}
	}
}



void Point_Water(RGB_Color_TypeDef Color)
{
	//提取键盘按下的值
	if(BTK05_ATKeyDataPack[4] != 0)
	{
		//控制按键部分
		if((BTK05_ATKeyDataPack[4] & 0x01) == 0x01)
		{
			NOW_ID_0[86].R  = Color.R;
			NOW_ID_0[86].G  = Color.G;
			NOW_ID_0[86].B  = Color.B;
			NOW_ID_0[86].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x02) == 0x02)
		{
			NOW_ID_0[63].R  = Color.R;
			NOW_ID_0[63].G  = Color.G;
			NOW_ID_0[63].B  = Color.B;
			NOW_ID_0[63].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x04) == 0x04)
		{
			NOW_ID_0[84].R  = Color.R;
			NOW_ID_0[84].G  = Color.G;
			NOW_ID_0[84].B  = Color.B;
			NOW_ID_0[84].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x08) == 0x08)
		{
			NOW_ID_0[85].R  = Color.R;
			NOW_ID_0[85].G  = Color.G;
			NOW_ID_0[85].B  = Color.B;
			NOW_ID_0[85].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x10) == 0x10)
		{
			NOW_ID_0[79].R  = Color.R;
			NOW_ID_0[79].G  = Color.G;
			NOW_ID_0[79].B  = Color.B;
			NOW_ID_0[79].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x20) == 0x20)
		{
			NOW_ID_0[74].R  = Color.R;
			NOW_ID_0[74].G  = Color.G;
			NOW_ID_0[74].B  = Color.B;
			NOW_ID_0[74].PWM_INDEX = 32;
		}
		if((BTK05_ATKeyDataPack[4] & 0x40) == 0x40)
		{
			NOW_ID_0[82].R  = Color.R;
			NOW_ID_0[82].G  = Color.G;
			NOW_ID_0[82].B  = Color.B;
			NOW_ID_0[82].PWM_INDEX = 32;
		}
	}
	//普通按键部分
	for(int index = 0;index < 6;index++)
	{
		if(BTK05_ATKeyDataPack[6+index] != 0x00)//如果控制字不为0，则查询相关对应按键的字。
		{
			for(int i = 0;i < 87;i++)
			{
				if(BTK05_ATKeyDataPack[6 + index] == sequence[i])
				{
					NOW_ID_0[i].R = Color.R;					
					NOW_ID_0[i].G = Color.G;
					NOW_ID_0[i].B = Color.B;
					NOW_ID_0[i].PWM_INDEX = 32;
				}
			}
		}
	}
	
	for(int index = 0;index < 87;index++)
	{
		if(NOW_ID_0[index].PWM_INDEX > 1)
		{
			for(int i = 0;i < 8;i++)
			{
				Pixel_Buf[index][i] = ((NOW_ID_0[index].G * NOW_ID_0[index].PWM_INDEX / 16 & ( 1 << (7 - i)))?(CODE_1):CODE_0);
			}
			for(int i = 8;i < 16;i++)
			{
				Pixel_Buf[index][i] = ((NOW_ID_0[index].R * NOW_ID_0[index].PWM_INDEX / 16 & ( 1 << (15 - i)))?(CODE_1):CODE_0);
			}
			for(int i = 16;i < 24;i++)
			{
				Pixel_Buf[index][i] = ((NOW_ID_0[index].B * NOW_ID_0[index].PWM_INDEX / 16 & ( 1 << (23 - i)))?(CODE_1):CODE_0);
			}
			NOW_ID_0[index].PWM_INDEX --; 
		}
		else if(NOW_ID_0[index].PWM_INDEX == 1)
		{
			if(NOW_ID_0[index].PWM_INDEX == NOW_ID_1[index].PWM_INDEX)
			{
				NOW_ID_0[index].PWM_INDEX --;
			}
			else
			{
				for(int i = 0;i < 8;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_0[index].G * 1/ 16 & ( 1 << (7 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 8;i < 16;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_0[index].R * 1/ 16 & ( 1 << (15 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 16;i < 24;i++)
				{
					Pixel_Buf[index][i] = ((NOW_ID_0[index].B * 1/ 16 & ( 1 << (23 - i)))?(CODE_1):CODE_0);
				}
			}
		}
	}
}

void Point_Water_Independent(RGB_Color_TypeDef Color)
{	
	if(Count_RGB_Flag == 4)
	{
		HAL_TIM_Base_Start_IT(&htim3);
		
		if(RGB_Change_Flag == 1)
		{
			//提取键盘按下的值
			RGB_Change_Flag = 0;
			if(BTK05_ATKeyDataPack[4] != 0)
			{
				//控制按键部分
				if((BTK05_ATKeyDataPack[4] & 0x01) == 0x01)
				{
					NOW_ID_2[86].R  = Color.R;
					NOW_ID_2[86].G  = Color.G;
					NOW_ID_2[86].B  = Color.B;
					NOW_ID_2[86].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x02) == 0x02)
				{
					NOW_ID_2[63].R  = Color.R;
					NOW_ID_2[63].G  = Color.G;
					NOW_ID_2[63].B  = Color.B;
					NOW_ID_2[63].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x04) == 0x04)
				{
					NOW_ID_2[84].R  = Color.R;
					NOW_ID_2[84].G  = Color.G;
					NOW_ID_2[84].B  = Color.B;
					NOW_ID_2[84].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x08) == 0x08)
				{
					NOW_ID_2[85].R  = Color.R;
					NOW_ID_2[85].G  = Color.G;
					NOW_ID_2[85].B  = Color.B;
					NOW_ID_2[85].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x10) == 0x10)
				{
					NOW_ID_2[79].R  = Color.R;
					NOW_ID_2[79].G  = Color.G;
					NOW_ID_2[79].B  = Color.B;
					NOW_ID_2[79].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x20) == 0x20)
				{
					NOW_ID_2[74].R  = Color.R;
					NOW_ID_2[74].G  = Color.G;
					NOW_ID_2[74].B  = Color.B;
					NOW_ID_2[74].PWM_INDEX = 32;
				}
				if((BTK05_ATKeyDataPack[4] & 0x40) == 0x40)
				{
					NOW_ID_2[82].R  = Color.R;
					NOW_ID_2[82].G  = Color.G;
					NOW_ID_2[82].B  = Color.B;
					NOW_ID_2[82].PWM_INDEX = 32;
				}
			}
			//普通按键部分
			for(int index = 0;index < 6;index++)
			{
				if(BTK05_ATKeyDataPack[6+index] != 0x00)//如果普通按键不为0，则查询相关对应按键的字。
				{
					for(int i = 0;i < 87;i++)
					{
						if(BTK05_ATKeyDataPack[6 + index] == sequence[i])
						{
							NOW_ID_2[i].R = Color.R;					
							NOW_ID_2[i].G = Color.G;
							NOW_ID_2[i].B = Color.B;
							NOW_ID_2[i].PWM_INDEX = 32;
						}
					}
				}
			}
			
			for(int index = 0;index < 87;index++)
			{
				if(NOW_ID_2[index].PWM_INDEX > 0)
				{
					for(int i = 0;i < 8;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].G * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (7 - i)))?(CODE_1):CODE_0);
					}
					for(int i = 8;i < 16;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].R * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (15 - i)))?(CODE_1):CODE_0);
					}
					for(int i = 16;i < 24;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].B * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (23 - i)))?(CODE_1):CODE_0);
					}
					NOW_ID_2[index].PWM_INDEX --;
				}
				else
				{
					for(int i = 0;i < 8;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].G * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (7 - i)))?(CODE_1):CODE_0);
					}
					for(int i = 8;i < 16;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].R * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (15 - i)))?(CODE_1):CODE_0);
					}
					for(int i = 16;i < 24;i++)
					{
						Pixel_Buf[index][i] = ((NOW_ID_2[index].B * NOW_ID_2[index].PWM_INDEX / 16 & ( 1 << (23 - i)))?(CODE_1):CODE_0);
					}
				}
			}
			RGB_SendArray();
		}
	}
}

void Choose_Color()
{
	if(Count_RGB_Flag == 5)
	{
		HAL_TIM_Base_Start_IT(&htim3);
		
		if(RGB_Change_Flag == 1)
		{
			RGB_Change_Flag = 0;
			uint8_t R_Index = 0;
			uint8_t G_Index = 0;
			uint8_t B_Index = 0;
			for(int index = 0;index < 24;index++)
			{
				Choose_Color_ID[index].R = (8 * COLOR_RING_BUFFER_1[index]);
				
				if(index + 8 < 24)
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_1[index + 8]);
				}
				else
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_1[index + 8 - 24]);
				}
				
				if(index + 16 < 24)
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_1[index + 16]);
				}
				else
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_1[index + 16 - 24]);
				}
			}
			
			for(int index = 24;index < 48;index++)
			{
				Choose_Color_ID[index].R = (8 * COLOR_RING_BUFFER_2[index - 24]);
				
				if(index + 8 < 48)
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_2[index + 8 -24]);
				}
				else
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_2[index + 8 - 48]);
				}
				
				if(index + 16 < 48)
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_2[index + 16 -24]);
				}
				else
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_2[index + 16 - 48]);
				}
			}
			
			for(int index = 48;index < 72;index++)
			{
				Choose_Color_ID[index].R = (8 * COLOR_RING_BUFFER_3[index - 48]);
				
				if(index + 8 < 72)
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_3[index + 8 -48]);
				}
				else
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_3[index + 8 - 72]);
				}
				
				if(index + 16 < 72)
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_3[index + 16 - 48]);
				}
				else
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_3[index + 16 - 72]);
				}
			}
			
			for(int index = 72;index < 88;index++)
			{
				Choose_Color_ID[index].R = (8 * COLOR_RING_BUFFER_3[index - 72]);
				
				if(index + 8 < 96)
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_3[index + 8 -72]);
				}
				else
				{
					Choose_Color_ID[index].G = (8 * COLOR_RING_BUFFER_3[index + 8 - 96]);
				}
				
				if(index + 16 < 96)
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_3[index + 16 - 72]);
				}
				else
				{
					Choose_Color_ID[index].B = (8 * COLOR_RING_BUFFER_3[index + 16 - 96]);
				}
			}
			
			for(int index = 0;index < 87;index++)
			{
				for(int i = 0;i < 8;i++)
				{
					Pixel_Buf[index][i] = ((Choose_Color_ID[index].G & ( 1 << (7 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 8;i < 16;i++)
				{
					Pixel_Buf[index][i] = ((Choose_Color_ID[index].R & ( 1 << (15 - i)))?(CODE_1):CODE_0);
				}
				for(int i = 16;i < 24;i++)
				{
					Pixel_Buf[index][i] = ((Choose_Color_ID[index].B & ( 1 << (23 - i)))?(CODE_1):CODE_0);
				}
			}
			
			RGB_SendArray();
			
			if(BTK05_ATKeyDataPack[4] != 0)
				{
					//控制按键部分
					if((BTK05_ATKeyDataPack[4] & 0x01) == 0x01)
					{
						RGB_STATE.R  = Choose_Color_ID[86].R;
						RGB_STATE.G  = Choose_Color_ID[86].G;
						RGB_STATE.B  = Choose_Color_ID[86].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x02) == 0x02)
					{
						RGB_STATE.R  = Choose_Color_ID[63].R;
						RGB_STATE.G  = Choose_Color_ID[63].G;
						RGB_STATE.B  = Choose_Color_ID[63].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x04) == 0x04)
					{
						RGB_STATE.R  = Choose_Color_ID[84].R;
						RGB_STATE.G  = Choose_Color_ID[84].G;
						RGB_STATE.B  = Choose_Color_ID[84].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x08) == 0x08)
					{
						RGB_STATE.R  = Choose_Color_ID[85].R;
						RGB_STATE.G  = Choose_Color_ID[85].G;
						RGB_STATE.B  = Choose_Color_ID[85].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x10) == 0x10)
					{
						RGB_STATE.R  = Choose_Color_ID[79].R;
						RGB_STATE.G  = Choose_Color_ID[79].G;
						RGB_STATE.B  = Choose_Color_ID[79].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x20) == 0x20)
					{
						RGB_STATE.R  = Choose_Color_ID[74].R;
						RGB_STATE.G  = Choose_Color_ID[74].G;
						RGB_STATE.B  = Choose_Color_ID[74].B;
						Count_RGB_Flag = 0;
					}
					if((BTK05_ATKeyDataPack[4] & 0x40) == 0x40)
					{
						RGB_STATE.R  = Choose_Color_ID[82].R;
						RGB_STATE.G  = Choose_Color_ID[82].G;
						RGB_STATE.B  = Choose_Color_ID[82].B;
						Count_RGB_Flag = 0;
					}
				}
				//普通按键部分
				for(int index = 0;index < 6;index++)
				{
					if(BTK05_ATKeyDataPack[6+index] != 0x00)//如果普通按键不为0，则查询相关对应按键的字。Q  
						
					{
						for(int i = 0;i < 87;i++)
						{
							if(BTK05_ATKeyDataPack[6 + index] == sequence[i])
							{
								RGB_STATE.R = Choose_Color_ID[i].R;					
								RGB_STATE.G = Choose_Color_ID[i].G;
								RGB_STATE.B = Choose_Color_ID[i].B;
								Count_RGB_Flag = 0;
							}
						}
					}
				}
			memset(Choose_Color_ID,0,sizeof(Choose_Color_ID));
			if(Count_RGB_Flag == 0)
			{
				HAL_Delay(200);
				Full_COLOR(RGB_STATE);
			}
		}
	}
}

void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
{
	HAL_TIM_PWM_Stop_DMA(&htim1,TIM_CHANNEL_1);
}


